package com.sg.service.biz.pbff.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.sg.common.util.RedisUtil;
import com.sg.dto.biz.pbff.req.*;
import com.sg.dto.biz.pbff.res.*;
import com.sg.service.biz.pbff.PowerService;
import com.wicket.okrframework.integration.InductionRecordService;
import com.wicket.okrframework.integration.PowerService;
import com.wicket.okrframework.integration.dto.*;
import com.sg.common.exception.Assert;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.stream.Collectors;

/**
 * @author ：
 * @version 1.0
 * @date ：
 */
@Service
public class PowerServiceImpl implements PowerService {

    @Resource
    com.sg.service.biz.framework.PowerService fwBasePowerClient;
    @Resource
    com.sg.service.biz.framework.InductionRecordService fwBaseInductionRecordClient;
    @Resource
    private RedisUtil redisUtil;

    /**
     * B1-2查询权限列表（平台）[558]
     * gen by moon at 8/10/2022, 3:40:28 AM
     */
    @Trace(operationName = "B1-2查询权限列表（平台）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    public BffQueryPowerListRespDto queryPowerList(BffQueryPowerListReqDto reqDto) {


        QueryPowerListRespDto queryPowerListRespDto_1 = null;
        BatchQueryInductionRecordListComRespDto batchQueryInductionRecordListComRespDto_1 = null;
//步骤0: D1-2查询权限列表 - queryPowerList
        QueryPowerListRespDto queryPowerListRespDto = null;
        QueryPowerListReqDto queryPowerListReqDto = new QueryPowerListReqDto();
        if (reqDto != null) {
            queryPowerListReqDto.setPrivacyType(reqDto.getPrivacyType());//sourceId:26328_1
            queryPowerListReqDto.setStrategyType(reqDto.getStrategyType());//sourceId:26329_1
            queryPowerListReqDto.setIsportPrivacy(reqDto.getIsportPrivacy());//sourceId:44175_1
            queryPowerListReqDto.setIsDataPrivacy(reqDto.getIsDataPrivacy());//sourceId:44176_1
            queryPowerListReqDto.setBusinessPurposes(reqDto.getBusinessPurposes());//sourceId:44177_1
            queryPowerListReqDto.setIsArchive(reqDto.getIsArchive());//sourceId:26333_1
        }

        /*D1-2查询权限列表[555]   */

        queryPowerListRespDto = fwBasePowerClient.queryPowerList(queryPowerListReqDto).getData();

        queryPowerListRespDto_1 = queryPowerListRespDto;

//步骤1: D1-3批量查询就职记录列表（公共） - batchQueryInductionRecordListCom
        BatchQueryInductionRecordListComRespDto batchQueryInductionRecordListComRespDto = null;
        if (queryPowerListRespDto != null) {
            BatchQueryInductionRecordListComReqDto batchQueryInductionRecordListComReqDto = new BatchQueryInductionRecordListComReqDto();
            if (queryPowerListRespDto != null) {
                batchQueryInductionRecordListComReqDto.setPerOrgInductionRecordList(queryPowerListRespDto.getFrameworkPrivacyList().stream().map(item -> item.getOperationInductionId())
                        .collect(Collectors.toList()));//sourceId:26338_1
            }

            /*D1-3批量查询就职记录列表（公共）[602]   */

            batchQueryInductionRecordListComRespDto = fwBaseInductionRecordClient.batchQueryInductionRecordListCom(batchQueryInductionRecordListComReqDto).getData();

            batchQueryInductionRecordListComRespDto_1 = batchQueryInductionRecordListComRespDto;
        }

        BffQueryPowerListRespDto retData = new BffQueryPowerListRespDto();
        //数据集融合
        if (queryPowerListRespDto_1 != null && !CollectionUtil.isEmpty(queryPowerListRespDto_1.getFrameworkPrivacyList())) {
            for (FrameworkPrivacyDto frameworkPrivacyDto : queryPowerListRespDto_1.getFrameworkPrivacyList()) {
                BffFrameworkPrivacyDto retElm = new BffFrameworkPrivacyDto();
                if (queryPowerListRespDto_1 != null) {
                    retElm.setPrivacyId(frameworkPrivacyDto.getPrivacyId());//sourceId:10732_2
                    retElm.setPrivacyCode(frameworkPrivacyDto.getPrivacyCode());//sourceId:10733_2
                    retElm.setPrivacyName(frameworkPrivacyDto.getPrivacyName());//sourceId:10734_2
                    retElm.setSubjectLifeCycle(frameworkPrivacyDto.getSubjectLifeCycle());//sourceId:10736_2
                    retElm.setOperationInductionId(frameworkPrivacyDto.getOperationInductionId());//sourceId:10737_2
                    retElm.setOperateTime(frameworkPrivacyDto.getOperateTime());//sourceId:10738_2
                }
                retData.getFrameworkPrivacyList().add(retElm);
                if (batchQueryInductionRecordListComRespDto_1 != null) {
                    for (PerOrgInductionRecordDto perOrgInductionRecordDto : batchQueryInductionRecordListComRespDto_1.getPerOrgInductionRecordList()) {
                        if (perOrgInductionRecordDto.getInductionRecordId().equals(frameworkPrivacyDto.getOperationInductionId())) {
                            if (batchQueryInductionRecordListComRespDto_1 != null) {
                                retElm.setUserId(perOrgInductionRecordDto.getUserId());//sourceId:38736_2
                                retElm.setUserFace(perOrgInductionRecordDto.getUserFace());//sourceId:38739_2
                                retElm.setIdCardName(perOrgInductionRecordDto.getIdCardName());//sourceId:38737_2
                                retElm.setNickName(perOrgInductionRecordDto.getNickName());//sourceId:38738_2
                            }
                        }
                    }
                }

            }
        }//sourceId:26349_1


        retData.setPageNum(queryPowerListRespDto.getPageNum());
        retData.setPageSize(queryPowerListRespDto.getPageSize());
        retData.setTotal(queryPowerListRespDto.getTotalNum());
        retData.setIsLastPage((Math.ceil(Double.valueOf(queryPowerListRespDto.getTotalNum()) / Double.valueOf(queryPowerListRespDto.getPageSize()))) <= retData.getPageNum());


        return retData;
    }

    /**
     * B1-2存档权限（平台）[570]
     * gen by moon at 8/31/2022, 7:28:46 PM
     */
    @Trace(operationName = "B1-2存档权限（平台）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    public BffArchivePowerRespDto archivePower(BffArchivePowerReqDto reqDto) {


        //步骤0: D1-2存档权限 - archivePower
        ArchivePowerRespDto archivePowerRespDto = null;
        ArchivePowerReqDto archivePowerReqDto = new ArchivePowerReqDto();
        if (reqDto != null) {
            archivePowerReqDto.setPrivacyId(reqDto.getPrivacyId());//sourceId:44605_1
        }

        /*D1-2存档权限[1549]   */
        Assert.isNull(archivePowerReqDto.getPrivacyId(), "B1-2存档权限（平台）-D1-2存档权限-权限ID不能为空", false);
        archivePowerRespDto = fwBasePowerClient.archivePower(archivePowerReqDto).getData();


//步骤1: D1-存档权限分配缓存 - archiveTributPowerRedis
        ArchiveTributPowerRedisRespDto archiveTributPowerRedisRespDto = null;
        ArchiveTributPowerRedisReqDto archiveTributPowerRedisReqDto = new ArchiveTributPowerRedisReqDto();
        if (reqDto != null) {
            archiveTributPowerRedisReqDto.setPrivacyId(reqDto.getPrivacyId());//sourceId:52783_1
        }

        /*D1-存档权限分配缓存[1725]   */
        Assert.isNull(archiveTributPowerRedisReqDto.getPrivacyId(), "B1-2存档权限（平台）-D1-存档权限分配缓存-权限ID不能为空", false);
        archiveTributPowerRedisRespDto = fwBasePowerClient.archiveTributPowerRedis(archiveTributPowerRedisReqDto).getData();


        BffArchivePowerRespDto retData = new BffArchivePowerRespDto();


        return retData;
    }

    /**
     * B1-2修改权限（平台）[564]
     * gen by moon at 8/10/2022, 3:40:32 AM
     */
    @Trace(operationName = "B1-2修改权限（平台）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    public BffUpdatePowerRespDto updatePower(BffUpdatePowerReqDto reqDto) {


        //步骤0: D1-2修改权限（平台） - updatePower
        UpdatePowerRespDto updatePowerRespDto = null;
        UpdatePowerReqDto updatePowerReqDto = new UpdatePowerReqDto();
        if (reqDto != null) {
            updatePowerReqDto.setPrivacyId(reqDto.getPrivacyId());//sourceId:11472_1
            updatePowerReqDto.setPrivacyName(reqDto.getPrivacyName());//sourceId:11473_1
            updatePowerReqDto.setPrivacyCode(reqDto.getPrivacyCode());//sourceId:28407_1
            updatePowerReqDto.setPrivacyIntroduce(reqDto.getPrivacyIntroduce());//sourceId:11474_1
            updatePowerReqDto.setPrivacyLogo(reqDto.getPrivacyLogo());//sourceId:11475_1
            updatePowerReqDto.setPrivacyType(reqDto.getPrivacyType());//sourceId:11480_1
            updatePowerReqDto.setStrategyType(reqDto.getStrategyType());//sourceId:11476_1
            updatePowerReqDto.setIsportPrivacy(reqDto.getIsportPrivacy());//sourceId:43873_1
            updatePowerReqDto.setPrivacyRoute(reqDto.getPrivacyRoute());//sourceId:43874_1
            updatePowerReqDto.setIsDataPrivacy(reqDto.getIsDataPrivacy());//sourceId:43875_1
            updatePowerReqDto.setDataModel(reqDto.getDataModel());//sourceId:34905_1
            updatePowerReqDto.setPrivacyCategory(reqDto.getPrivacyCategory());//sourceId:43876_1
            updatePowerReqDto.setBusinessPurposes(reqDto.getBusinessPurposes());//sourceId:43877_1
            updatePowerReqDto.setSubjectLifeCycle(reqDto.getSubjectLifeCycle());//sourceId:11479_1
        }

        /*D1-2修改权限（平台）[563]   */
        Assert.isNull(updatePowerReqDto.getPrivacyId(), "B1-2修改权限（平台）-D1-2修改权限（平台）-权限ID不能为空", false);
        updatePowerRespDto = fwBasePowerClient.updatePower(updatePowerReqDto).getData();


        BffUpdatePowerRespDto retData = new BffUpdatePowerRespDto();


        return retData;
    }

    /**
     * B1-2新增权限（平台）[562]
     * gen by moon at 11/9/2022, 9:14:02 PM
     */
    @Trace(operationName = "B1-2新增权限（平台）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    public BffAddPowerRespDto addPower(BffAddPowerReqDto reqDto) {


        AddPowerRespDto addPowerRespDto_1 = null;
//步骤0: D1-2新增权限(平台) - addPower
        AddPowerRespDto addPowerRespDto = null;
        AddPowerReqDto addPowerReqDto = new AddPowerReqDto();
        if (reqDto != null) {
            addPowerReqDto.setPrivacyCode(reqDto.getPrivacyCode());//SimpleFieldAssign//sourceId:11425_1
            addPowerReqDto.setPrivacyName(reqDto.getPrivacyName());//SimpleFieldAssign//sourceId:11418_1
            addPowerReqDto.setPrivacyIntroduce(reqDto.getPrivacyIntroduce());//SimpleFieldAssign//sourceId:11419_1
            addPowerReqDto.setPrivacyLogo(reqDto.getPrivacyLogo());//SimpleFieldAssign//sourceId:11420_1
            addPowerReqDto.setPrivacyType(reqDto.getPrivacyType());//SimpleFieldAssign//sourceId:11421_1
            addPowerReqDto.setStrategyType(reqDto.getStrategyType());//SimpleFieldAssign//sourceId:11438_1
            addPowerReqDto.setIsportPrivacy(reqDto.getIsportPrivacy());//SimpleFieldAssign//sourceId:43840_1
            addPowerReqDto.setPrivacyRoute(reqDto.getPrivacyRoute());//SimpleFieldAssign//sourceId:43841_1
            addPowerReqDto.setIsDataPrivacy(reqDto.getIsDataPrivacy());//SimpleFieldAssign//sourceId:43842_1
            addPowerReqDto.setDataModel(reqDto.getDataModel());//SimpleFieldAssign//sourceId:34946_1
            addPowerReqDto.setPrivacyCategory(reqDto.getPrivacyCategory());//SimpleFieldAssign//sourceId:43843_1
            addPowerReqDto.setBusinessPurposes(reqDto.getBusinessPurposes());//SimpleFieldAssign//sourceId:43844_1
            addPowerReqDto.setSubjectLifeCycle(reqDto.getSubjectLifeCycle());//SimpleFieldAssign//sourceId:11424_1
        }

        /*D1-2新增权限(平台)[561]   */
        Assert.isNull(addPowerReqDto.getPrivacyCode(), "B1-2新增权限（平台）-D1-2新增权限(平台)-权限标识不能为空", false);
        Assert.isNull(addPowerReqDto.getPrivacyName(), "B1-2新增权限（平台）-D1-2新增权限(平台)-权限名称不能为空", false);
        Assert.isNull(addPowerReqDto.getPrivacyType(), "B1-2新增权限（平台）-D1-2新增权限(平台)-权限类型不能为空", false);
        Assert.isNull(addPowerReqDto.getStrategyType(), "B1-2新增权限（平台）-D1-2新增权限(平台)-分配策略不能为空", false);
        Assert.isNull(addPowerReqDto.getIsportPrivacy(), "B1-2新增权限（平台）-D1-2新增权限(平台)-是否接口权限不能为空", false);
        Assert.isNull(addPowerReqDto.getIsDataPrivacy(), "B1-2新增权限（平台）-D1-2新增权限(平台)-是否数据权限不能为空", false);
        Assert.isNull(addPowerReqDto.getSubjectLifeCycle(), "B1-2新增权限（平台）-D1-2新增权限(平台)-主体生命周期不能为空", false);
        addPowerRespDto = fwBasePowerClient.addPower(addPowerReqDto).getData();


        addPowerRespDto_1 = addPowerRespDto;

        BffAddPowerRespDto retData = new BffAddPowerRespDto();
        if (addPowerRespDto_1 != null) {
            retData.setPrivacyId(addPowerRespDto_1.getPrivacyId());//SimpleFieldAssign//sourceId:11435_1
        }


        return retData;
    }

    /**
     * B1-2查询权限详情（平台）[560]
     * gen by moon at 8/10/2022, 3:40:29 AM
     */
    @Trace(operationName = "B1-2查询权限详情（平台）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    public BffQueryPowerDetailRespDto queryPowerDetail(BffQueryPowerDetailReqDto reqDto) {


        QueryPowerDetailRespDto queryPowerDetailRespDto_1 = null;
        QueryInductionRecordInfoDetailComRespDto queryInductionRecordInfoDetailComRespDto_1 = null;
//步骤0: D1-2查询权限详情 - queryPowerDetail
        QueryPowerDetailRespDto queryPowerDetailRespDto = null;
        QueryPowerDetailReqDto queryPowerDetailReqDto = new QueryPowerDetailReqDto();
        if (reqDto != null) {
            queryPowerDetailReqDto.setPrivacyId(reqDto.getPrivacyId());//sourceId:25958_1
        }

        /*D1-2查询权限详情[559]   */
        Assert.isNull(queryPowerDetailReqDto.getPrivacyId(), "B1-2查询权限详情（平台）-D1-2查询权限详情-权限ID不能为空", false);
        queryPowerDetailRespDto = fwBasePowerClient.queryPowerDetail(queryPowerDetailReqDto).getData();

        queryPowerDetailRespDto_1 = queryPowerDetailRespDto;

//步骤1: D1-3查询个人就职记录信息详情（公共） - queryInductionRecordInfoDetailCom
        QueryInductionRecordInfoDetailComRespDto queryInductionRecordInfoDetailComRespDto = null;
        if (queryPowerDetailRespDto != null) {
            QueryInductionRecordInfoDetailComReqDto queryInductionRecordInfoDetailComReqDto = new QueryInductionRecordInfoDetailComReqDto();
            if (queryPowerDetailRespDto != null) {
                queryInductionRecordInfoDetailComReqDto.setInductionRecordId(queryPowerDetailRespDto.getOperationInductionId());//sourceId:26093_1
            }

            /*D1-3查询个人就职记录信息详情（公共）[715]   */
            Assert.isNull(queryInductionRecordInfoDetailComReqDto.getInductionRecordId(), "B1-2查询权限详情（平台）-D1-3查询个人就职记录信息详情（公共）-就职记录ID不能为空", false);
            queryInductionRecordInfoDetailComRespDto = fwBaseInductionRecordClient.queryInductionRecordInfoDetailCom(queryInductionRecordInfoDetailComReqDto).getData();

            queryInductionRecordInfoDetailComRespDto_1 = queryInductionRecordInfoDetailComRespDto;
        }

        BffQueryPowerDetailRespDto retData = new BffQueryPowerDetailRespDto();
        if (queryPowerDetailRespDto_1 != null) {
            retData.setPrivacyId(queryPowerDetailRespDto_1.getPrivacyId());//sourceId:26102_1
            retData.setPrivacyCode(queryPowerDetailRespDto_1.getPrivacyCode());//sourceId:26103_1
            retData.setPrivacyName(queryPowerDetailRespDto_1.getPrivacyName());//sourceId:26104_1
            retData.setPrivacyIntroduce(queryPowerDetailRespDto_1.getPrivacyIntroduce());//sourceId:26105_1
            retData.setPrivacyLogo(queryPowerDetailRespDto_1.getPrivacyLogo());//sourceId:26107_1
            retData.setPrivacyType(queryPowerDetailRespDto_1.getPrivacyType());//sourceId:26109_1
            retData.setStrategyType(queryPowerDetailRespDto_1.getStrategyType());//sourceId:26111_1
            retData.setIsportPrivacy(queryPowerDetailRespDto_1.getIsportPrivacy());//sourceId:44199_1
            retData.setPrivacyRoute(queryPowerDetailRespDto_1.getPrivacyRoute());//sourceId:26112_1
            retData.setIsDataPrivacy(queryPowerDetailRespDto_1.getIsDataPrivacy());//sourceId:44200_1
            retData.setDataModel(queryPowerDetailRespDto_1.getDataModel());//sourceId:26113_1
            retData.setPrivacyCategory(queryPowerDetailRespDto_1.getPrivacyCategory());//sourceId:44201_1
            retData.setBusinessPurposes(queryPowerDetailRespDto_1.getBusinessPurposes());//sourceId:44202_1
            retData.setSubjectLifeCycle(queryPowerDetailRespDto_1.getSubjectLifeCycle());//sourceId:26114_1
            retData.setOperationInductionId(queryPowerDetailRespDto_1.getOperationInductionId());//sourceId:26115_1
            retData.setOperateTime(queryPowerDetailRespDto_1.getOperateTime());//sourceId:34887_1
        }
        if (queryInductionRecordInfoDetailComRespDto_1 != null) {
            retData.setUserId(queryInductionRecordInfoDetailComRespDto_1.getUserId());//sourceId:34777_1
            retData.setUserFace(queryInductionRecordInfoDetailComRespDto_1.getUserFace());//sourceId:26110_1
            retData.setIdCardName(queryInductionRecordInfoDetailComRespDto_1.getIdCardName());//sourceId:26106_1
            retData.setNickName(queryInductionRecordInfoDetailComRespDto_1.getNickName());//sourceId:26108_1
        }


        return retData;
    }

    /**
     * B1-2删除权限（平台）[5170]
     * gen by moon at 11/8/2022, 7:49:59 PM
     */
    @Trace(operationName = "B1-2删除权限（平台）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    public BffDeletePowerRespDto deletePower(BffDeletePowerReqDto reqDto) {


        //步骤0: D1-2删除权限 - deletePower
        DeletePowerRespDto deletePowerRespDto = null;
        DeletePowerReqDto deletePowerReqDto = new DeletePowerReqDto();
        if (reqDto != null) {
            deletePowerReqDto.setPrivacyId(reqDto.getPrivacyId());//SimpleFieldAssign//sourceId:425526_1
        }

        /*D1-2删除权限[565]   */
        Assert.isNull(deletePowerReqDto.getPrivacyId(), "B1-2删除权限（平台）-D1-2删除权限-权限ID不能为空", false);
        deletePowerRespDto = fwBasePowerClient.deletePower(deletePowerReqDto).getData();


        BffDeletePowerRespDto retData = new BffDeletePowerRespDto();


        return retData;
    }
    //
}
